package tt.dz.biz.service.account;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.account.AccountingDAO;
import tt.dz.biz.service.common.CommonService;
import tt.dz.common.LogUtil;
import tt.dz.vo.AccountingRuleVO;
import tt.dz.vo.SubAccountingRuleVO;

@Service("accountService")
public class AccountService
{
  @Resource(name="accountingRuleDAO")
  AccountingDAO accountingDAO;
  @Resource(name="commonService")
  CommonService commonService;
  
  public List<AccountingRuleVO> listAllAccountingRule()
  {
    List<AccountingRuleVO> accountingRuleList = null;
    try
    {
      accountingRuleList = this.accountingDAO.listAllAccountingRule();
      if (accountingRuleList != null)
      {
        Iterator<AccountingRuleVO> iterator = accountingRuleList.iterator();
        while (iterator.hasNext())
        {
          AccountingRuleVO accountingRuleVO = (AccountingRuleVO)iterator.next();
          SubAccountingRuleVO paramVO = new SubAccountingRuleVO();
          paramVO.setRuleSeq(accountingRuleVO.getRuleSeq());
          accountingRuleVO.setSubAccountingRuleSet(this.accountingDAO.querySubAccountRule(paramVO));
        }
      }
    }
    catch (Exception e)
    {
      LogUtil.error("查询所有记账规则异常", e);
      accountingRuleList = new ArrayList();
    }
    return accountingRuleList;
  }
  
  public boolean insertAccountingRule(AccountingRuleVO accountingRuleVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.accountingDAO.insertAccountingRule(accountingRuleVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      LogUtil.error("插入记账规则异常", e);
    }
    return excuteFlag;
  }
  
  public boolean updateAccountingRule(AccountingRuleVO accountingRuleVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.accountingDAO.updateAccountingRule(accountingRuleVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      LogUtil.error("更新记账规则异常", e);
    }
    return excuteFlag;
  }
  
  public boolean insertSubAccountRule(SubAccountingRuleVO subAccountingRuleVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.accountingDAO.insertSubAccountRule(subAccountingRuleVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      LogUtil.error("插入子记账规则异常", e);
    }
    return excuteFlag;
  }
  
  public boolean updateSubAccountingRule(SubAccountingRuleVO subAccountingRuleVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.accountingDAO.updateSubAccountingRule(subAccountingRuleVO);
      excuteFlag = i > 0;
    }
    catch (Exception e)
    {
      LogUtil.error("更新子记账规则异常", e);
    }
    return excuteFlag;
  }
  
  public AccountingRuleVO queryRecieverAccountingRule(String serviceNo, String subServiceNo)
    throws Exception
  {
    try
    {
      AccountingRuleVO p = new AccountingRuleVO();
      p.setServiceNo(serviceNo);
      p.setSubServiceNo(subServiceNo);
      return this.accountingDAO.queryRecieverAccountingRule(p);
    }
    catch (Exception e)
    {
      LogUtil.error("根据服务号查询记账规则", e);
      throw e;
    }
  }
}
